package com.jlastu.znb.adapter;


import static android.view.View.GONE;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jlastu.znb.R;
import com.jlastu.znb.zj.zjItem;

import java.util.List;
import java.util.Locale;

public class zjAdapter extends ArrayAdapter<zjItem> {
    private Context context;
    private List<zjItem> zjItems;

    public zjAdapter(Context context, List<zjItem> zjItems) {
        super(context, R.layout.zj_item, zjItems);
        this.context = context;
        this.zjItems = zjItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // 复用convertView以优化性能
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.zj_item, parent, false);
        }

        zjItem zjItem = zjItems.get(position);
        ImageView itemImageView = convertView.findViewById(R.id.imageViewProduct9);
        TextView itemNameView = convertView.findViewById(R.id.zj_item_name9);
        TextView itemPriceView = convertView.findViewById(R.id.zj_item_price9);
        TextView itemQuantityView = convertView.findViewById(R.id.zj_item_quantity9);
        Bitmap bitmap = zjItem.getBitmap();
        if (!zjItem.hasImage()) {
            convertView.setVisibility(GONE);
            itemImageView.setVisibility(GONE);
        } else {
            convertView.setVisibility(View.VISIBLE);
            itemImageView.setImageBitmap(zjItem.getBitmap());
        }
        if (itemImageView != null && bitmap != null) {
            itemImageView.setImageBitmap(bitmap);
        } else if (itemImageView != null ){
            itemImageView.setImageResource(R.drawable.xiandou);
        }
        itemNameView.setText(zjItem.getGoodsName());
        itemPriceView.setText(String.format(Locale.getDefault(), "¥ %.2f", zjItem.getGoodsPrice()));
        itemQuantityView.setText(String.valueOf(zjItem.getQuantity()));

        return convertView;
    }
}